//index.js
//获取应用实例
let app = getApp();
var amapFile = require('../../../util/amap-wx.js');
let wechat = require("../../../util/wechat");
let amap = require("../../../util/amap");
let markersData = [];
Page({
  data: {
    blankSpaces:20,
    currentCity:'',
    markers: [],
    latitude: '',
    longitude: '',
    textData: {},
    city: '',
    markerId: 0,
    click1:false,
    click2:false,
    click3:false,
    controls: [
      {
        id: 0,
        position: {
          left: 10,
          top: 200,
          width: 40,
          height: 40
        },
        iconPath: "../images/首页图标1.png",
        clickable: true
      }
    ]
  },
  //地图推荐按钮跳转
  redirectToTab: function() {
    wx.switchTab({
      url: '/pages/search/search',
    });
  },
  redirectToJingdian: function() {
    wx.switchTab({
      url: '/pages/nearby/nearby',
    });
  },
  onLoad() {
    this.getCurrentCity(); // 在页面加载时获取当前城市信息
    var myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });
    amap.getRegeo()
      .then(d => {
        console.log(d);
        let { name, desc, latitude, longitude } = d[0];
        let { city } = d[0].regeocodeData.addressComponent;
        this.setData({
          city,
          latitude,
          longitude,
          textData: { name, desc }
        })
      })
      .catch(e => {
        console.log(e);
      })
  },
  
  clickcontrol(e) {
    console.log("回到用户当前定位点");
    let { controlId } = e;
    let mpCtx = wx.createMapContext("map");
    mpCtx.moveToLocation();
  },
  
   // 获取城市消息
  getCurrentCity() {
    let that = this;
    let myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });

    // 使用小程序的定位功能获取用户当前的经纬度信息
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        let { latitude, longitude } = res;

        // 调用逆地理编码接口获取当前城市信息
        amap.getRegeo(latitude, longitude)
          .then(d => {
            let { city } = d[0].regeocodeData.addressComponent;

            // 更新页面数据，将当前城市信息显示在页面上
            that.setData({
              currentCity: city
            });
          })
          .catch(e => {
            console.log("逆地理编码失败：", e);
          });
      },
      fail() {
        console.log("获取位置信息失败");
      }
    });
  },
  bindInput() {
    let { latitude, longitude, city } = this.data;
    let url = `/pages/map/inputtip/inputtip?city=${city}&lonlat=${longitude},${latitude}`;
    wx.navigateTo({ url });
  },
  makertap(e) {
    // console.log(e);
    let { markerId } = e;
    let { markers } = this.data;
    let marker = markers[markerId];
    // console.log(marker);
    this.showMarkerInfo(marker);
    this.changeMarkerColor(markerId);
  },
  showMarkerInfo(data) {
    let { name, address: desc } = data;
    this.setData({
      textData: { name, desc }
    })
  },
  changeMarkerColor(markerId) {
    let { markers } = this.data;
    markers.forEach((item, index) => {
      item.iconPath = "../images/Bluemaker.png";
      if (index == markerId) item.iconPath = "../images/Redmaker.png";
    })
    this.setData({ markers, markerId });
  },
  getRoute() {
    let that = this;
    // 获取用户当前位置的经纬度信息
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const { latitude, longitude } = res;
  
        // 更新页面数据中的用户当前位置的经纬度
        that.setData({
          latitude,
          longitude
        });
  
        // 获取其他需要的参数信息
        let { markers, markerId, city, textData } = that.data;
        let { name, desc } = textData;
  
        if (!markers.length) return;
  
        // 终点
        let { latitude: latitude2, longitude: longitude2 } = markers[markerId];
  
        // console.log(latitude);
        // console.log(longitude);
        // console.log(latitude2);
        // console.log(longitude2);
  //传递参数
        let url = `/pages/map/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${desc}`;
        wx.navigateTo({ url });
      },
      fail(error) {
        console.error('获取用户位置失败：', error);
      }
    });
  },
  
  clickcontrol(e) {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });
    amap.getRegeo()
      .then(d => {
        console.log(d);
        let { name, desc, latitude, longitude } = d[0];
        let { city } = d[0].regeocodeData.addressComponent;
        this.setData({
          city,
          latitude,
          longitude,
          textData: { name, desc }
        })
      })
      .catch(e => {
        console.log(e);
      })
    console.log("回到用户当前定位点");
    let { controlId } = e;
    let mpCtx = wx.createMapContext("map");
    mpCtx.moveToLocation();
  },
  mapchange() {
    // console.log("改变视野");
  },
  clickHotel: function () {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });
    var click1 = this.data.click1;
    var bol = !click1;
    myAmapFun.getPoiAround({
      iconPathSelected: '../images/Redmaker.png',
      iconPath: '../images/Bluemaker.png',
      querykeywords:'酒店',
      success: function (data) {
        markersData = data.markers;
        that.setData({
          markers: markersData
        });
        that.setData({
          latitude: markersData[0].latitude
        });
        that.setData({
          longitude: markersData[0].longitude
        });
        that.setData({
          click1: bol,
          click2:false,
          click3:false,
          click4:false,
          click5:false
        });
        that.showMarkerInfo(markersData, 0);
      },
      fail: function (info) {
        wx.showModal({ title: info.errMsg })
      }
    })
  },
  clickFood: function () {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });
    var click2 = this.data.click2;
    var bol = !click2;
    myAmapFun.getPoiAround({
      iconPathSelected: '../images/Redmaker.png',
      iconPath: '../images/Bluemaker.png',
      querykeywords: '美食',
      success: function (data) {
        markersData = data.markers;
        that.setData({
          markers: markersData
        });
        that.setData({
          latitude: markersData[0].latitude
        });
        that.setData({
          longitude: markersData[0].longitude
        });
        that.setData({
          click2:bol,
          click1:false,
          click3:false,
          click4:false,
          click5:false
        });
        that.showMarkerInfo(markersData, 0);
      },
      fail: function (info) {
        wx.showModal({ title: info.errMsg })
      }
    })
  },

//   clickService: function() {
//     var that = this;
//     var myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });
//     var click3 = this.data.click3;
//     var bol = !click3;
//     myAmapFun.getPoiAround({
//       iconPathSelected: '../images/Redmaker.png',
//       iconPath: '../images/Bluemaker.png',
//       querykeywords: '服务',
//       success: function (data) {
//         markersData = data.markers;
//         that.setData({
//           markers: markersData
//         });
//         that.setData({
//           latitude: markersData[0].latitude
//         });
//         that.setData({
//           longitude: markersData[0].longitude
//         });
//         that.setData({
//           click3:bol,
//           click1:false,
//           click2:false,
//           click4:false
//         });
//         that.showMarkerInfo(markersData, 0);
//       },
//       fail: function (info) {
//         wx.showModal({ title: info.errMsg })
//       }
//     })
//   },

  clickScenic: function() {
    var that = this;
    //高德地图key
    var myAmapFun = new amapFile.AMapWX({ key: 'd1a455cb6bff6b40d713b42a3023d257' });
    var click4 = this.data.click4;
    var bol = !click4;
    myAmapFun.getPoiAround({
      iconPathSelected: '../images/Redmaker.png',
      iconPath: '../images/Bluemaker.png',
      querykeywords: '景区',
      success: function (data) {
        markersData = data.markers;
        that.setData({
          markers: markersData
        });
        that.setData({
          latitude: markersData[0].latitude
        });
        that.setData({
          longitude: markersData[0].longitude
        });
        that.setData({
          click4:bol,
          click1:false,
          click2:false,
          click3:false,
          click5:false
        });
        that.showMarkerInfo(markersData, 0);
      },
      fail: function (info) {
        wx.showModal({ title: info.errMsg })
      }
    })
  },
  clickWeather: function () {
      // TODO: 有一个weather组件，当点击“天气”时跳转该页面
      wx.navigateTo({
        url: '/pages/weather/weather',
      })
  },
  showAwaitToast() {
    const user = wx.getStorageSync('user');
    if (!user) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/my/login/login'
            });
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '/pages/search/lasttime/lasttime'
      });
    }
  }
})
